package com.company.eduservice.controller;


import com.company.eduservice.entity.EduCourse;
import com.company.eduservice.service.EduCourseService;
import com.company.eduservice.service.EduTeacherService;
import com.company.eduservice.vo.course.CourseInfoVo;
import com.company.eduservice.vo.course.CourseNotarizeVo;
import com.company.servicebase.to.order.CourseTo;
import com.company.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-04-18
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Resource
    private EduCourseService eduCourseService;

    @Resource
    private EduTeacherService teacherService;

    //获得所有课程
    @GetMapping
    public R getCourseList() {
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list", list);
    }

    //添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        //返回添加之后课程id，为了后面添加大纲使用
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", id);
    }

    //确认课程信息回溯
    @GetMapping("/getPublishCourseInfo/{id}")
    public R courseNotarize(@PathVariable String id) {
        CourseNotarizeVo courseNotarizeVo = eduCourseService.courseNotarizeById(id);
        return R.ok().data("publishCourse", courseNotarizeVo);
    }

    //课程确认发布
    @GetMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    //删除课程
    @GetMapping("/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        eduCourseService.deleteCourseById(courseId);
        return R.ok();
    }

    //根据课程ID查询课程(限作用于订单生产所需)
    @GetMapping("/getCourse/{id}")
    public CourseTo getCourseById(@PathVariable("id") String id) {
        EduCourse course = eduCourseService.getById(id);
        CourseTo courseTo = new CourseTo();
        BeanUtils.copyProperties(course, courseTo);
        courseTo.setTeacherName(teacherService.getById(courseTo.getTeacherId()).getName());
        return courseTo;
    }

    //获取所有课程的播放数量
    @GetMapping("/CourseView")
    public Integer getCourseViewCount() {
        Integer count = eduCourseService.getCourseViewCount();
        return count;
    }

}

